﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)//判断页面是否首次被加载
        {
            initDate();
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        initDate();
        Literal1.Text = "<B>计算结果</B>";
    }

    private void initDate()
    {
        DateTime now = DateTime.Now;
        DateTime sDate = new DateTime(now.Year, now.Month, 1);
        DateTime eDate = now.AddDays(-(double)now.DayOfWeek);
        this.TextBox1.Text = sDate.ToString("yyyyMMdd");
        this.TextBox2.Text = eDate.ToString("yyyyMMdd");
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        string text1 = this.TextBox1.Text;
        string text2 = this.TextBox2.Text;
        try
        {
            DateTime date1 = txtToDate(text1);
            DateTime date2 = txtToDate(text2);
            AnniLaborCalendar calendar = new AnniLaborCalendar();
            ManMonths manMonths = new ManMonths(date1, date2);
            calendar.CreateManMonths(manMonths);
            Literal1.Text = "<B>计算结果</B></br>"
                + "总人月：" + manMonths.TotleManMonths + "人月</br>"
                + "总人日：" + manMonths.TotleDays + "人日</br>"
                + "期初（" + date1.Month + "月）：" + manMonths.StartMonthDays + "人日</br>"
                + "期末（" + date2.Month + "月）：" + manMonths.EndMonthDays + "人日</br>"
                + "中间月数：" + manMonths.MiddleMonths + "个月</br>";
        }
        catch (Exception)
        {
            Literal1.Text = "转换错误!";
        }
    }

    private DateTime txtToDate(string text1)
    {
        int year = int.Parse(text1.Substring(0, 4));
        int month = int.Parse(text1.Substring(4, 2));
        int day = int.Parse(text1.Substring(6, 2));
        DateTime date = new DateTime(year, month, day);
        return date;
    }
}